<?php
require_once('functions.php');

// ORDERFUNCTIONS
function GetOrders(){
	$conn = GetConnection();
	return $results = $conn -> query('SELECT * FROM WSP2012_Orders');
}

function GetOrder($id){
	$conn= GetConnection();
	$sql = "SELECT * FROM WSP2012_Orders WHERE order_id=$id ";
	$results = $conn -> query($sql);
	$row = $results -> fetch_assoc();
	$conn -> close();
	return $row;
}

function SaveOrder($row){
	$conn = GetConnection();
	$row2 = EscapeRow($row, $conn);
	$sql = "UPDATE `WSP2012_Orders` " 
		.	"Set users_id='$row2[users_id]', status_id='$row2[status_id]', updated_at='$row2[updated_at]', order_date='$row2[order_date]', "
		.	"ship_date='$row2[ship_date]', total_price='$row2[total_price]' " 	
		.	"WHERE order_id='$row2[id]' ";
	$results = $conn -> query($sql);
	$error = $conn -> error;
	$conn -> close();
	
	return $error == '' ? true : array('SQL Error' => $error);	
}

function CreateOrder($row){
	$conn = GetConnection();
	$row2 = EscapeRow($row, $conn);
	$sql = "INSERT INTO `WSP2012_Orders` "
		.		"(order_id, users_id, status_id, created_at, updated_at, order_date, ship_date, total_price) "
		.		"VALUES ('$row2[order_id]', '$row2[users_id]', '$row2[status_id]', '$row2[created_at]', '$row2[updated_at]', '$row2[order_date]', '$row2[ship_date]', '$row2[total_price]') ";
	$results = $conn -> query($sql);
	$error = $conn -> error;
	$conn -> close();	
	
	return $error == '' ? true : array('SQL Error' => $error);
}

function DeleteOrder($row, $id){
	$conn = GetConnection();
	$row2 = EscapeRow($row, $conn);
	$sql = "DELETE from WSP2012_Orders WHERE order_id= '$row2[id]' ";
	$results = $conn -> query($sql);
	$error = $conn -> error;
	$conn -> close();	
	
	return $error == '' ? true : array('SQL Error' => $error);
}
?>
